-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
231 lines (193 loc) · 8.03 KB
/
Copy pathmain.py
File metadata and controls
231 lines (193 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import datetime
from discord.ext import commands,tasks
import discord
import asyncio
import aiosqlite
import random
import requests
import praw
CHANNEL_ID=('1271180861949349911')
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
# Function to initialize the database
async def init_db():
conn = await aiosqlite.connect('bot_data.db')
c = await conn.cursor()
await c.execute('''
CREATE TABLE IF NOT EXISTS settings (
guild_id INTEGER PRIMARY KEY,
welcome_channel_id INTEGER
)
''')
await conn.commit()
await conn.close()
reddit = praw.Reddit(
client_id='-6EDwE2Vqm6Qxg_DSe2oMA',
client_secret='_TxTl28UsAF0L2FtaVHcetQzojPmfw',
user_agent='discord:my_meme_bot:v1.0'
)
@bot.event
async def on_ready():
print("Bot is ready!")
await init_db()
async with aiosqlite.connect('bot_data.db') as db:
async with db.execute('SELECT guild_id, welcome_channel_id FROM settings') as cursor:
async for row in cursor:
guild_id, channel_id = row
guild = bot.get_guild(guild_id)
if guild:
global welcome_channel_id
welcome_channel_id = channel_id
print("Welcome channels loaded from the database.")
@bot.command()
async def ping(ctx):
channel = await bot.fetch_channel(CHANNEL_ID)
await ctx.send('Pong!')
print(f'Sent by {ctx.author}')
@bot.command()
async def sum(ctx, *arr):
result=0
for i in arr:
result += int(i)
await ctx.send(f"Result: {result}")
print(f'Sent by {ctx.author}')
@bot.command()
@commands.has_role('Administrator') # Ensure only users with "Moderator" role can use this
async def assign_role(ctx, member: discord.Member, role: discord.Role):
# Check if the role is higher than the bot's role
if role.position >= ctx.guild.me.top_role.position:
await ctx.send("I cannot assign a role that is higher than my own.")
return
try:
await member.add_roles(role)
await ctx.send(f"Successfully assigned {role.name} to {member.name}.")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
@bot.command()
@commands.has_role('Administrator')
async def remove_role(ctx, member: discord.Member, role: discord.Role):
# Check if the role is higher than the bot's role
if role.position >= ctx.guild.me.top_role.position:
await ctx.send("I cannot remove a role that is higher than my own.")
return
try:
await member.remove_roles(role)
await ctx.send(f"Successfully removed {role.name} from {member.name}.")
except Exception as e:
await ctx.send(f"An error occurred: {e}")
# Error handling for permission issues
@assign_role.error
async def assign_role_error(ctx, error):
if isinstance(error, commands.MissingRole):
await ctx.send("You need the Moderator role to use this command.")
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send("Please specify a member and a role.")
else:
await ctx.send("An error occurred while assigning the role.")
@bot.command()
@commands.has_permissions(administrator=True)
async def set_welcome_channel(ctx, channel: discord.TextChannel):
guild_id = ctx.guild.id
channel_id = channel.id
async with aiosqlite.connect('bot_data.db') as db:
await db.execute('REPLACE INTO settings (guild_id, welcome_channel_id) VALUES (?, ?)', (guild_id, channel_id))
await db.commit()
await ctx.send(f'Welcome channel has been set to {channel.mention}')
@bot.event
async def on_member_join(member):
global welcome_channel_id
if welcome_channel_id:
channel = bot.get_channel(welcome_channel_id)
if channel:
emb = discord.Embed(
title="NEW MEMBER",
description=f"Thanks {member.mention} for joining!",
colour=discord.Colour.blue()
)
emb.set_image(url='https://example.com/image.jpg')
emb.add_field(name="Getting Started", value="Check out the rules channel to get familiar with our community!", inline=False)
await channel.send(embed=emb)
@bot.command(name='kick', pass_context=True)
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
await member.kick(reason=reason)
await ctx.send(f'User {member} has been kicked.')
@bot.command(name='ban', pass_context=True)
@commands.has_permissions(ban_members=True)
async def ban(ctx, member: discord.Member, *, reason=None):
await member.ban(reason=reason)
await ctx.send(f'User {member} has been banned.')
@bot.command(name='unban', pass_context=True)
@commands.has_permissions(ban_members=True)
async def unban(ctx, *, member):
banned_users = await ctx.guild.bans()
member_name, member_discriminator = member.split('#')
for ban_entry in banned_users:
user = ban_entry.user
if (user.name, user.discriminator) == (member_name, member_discriminator):
await ctx.guild.unban(user)
await ctx.send(f'User {user} has been unbanned.')
return
@bot.command(name='mute', pass_context=True)
@commands.has_permissions(manage_roles=True)
async def mute(ctx, member: discord.Member, duration: int = 0, *, reason=None):
muted_role = discord.utils.get(ctx.guild.roles, name='Muted')
if not muted_role:
muted_role = await ctx.guild.create_role(name='Muted')
for channel in ctx.guild.channels:
await channel.set_permissions(muted_role, speak=False, send_messages=False)
await member.add_roles(muted_role, reason=reason)
await ctx.send(f'User {member} has been muted.')
if duration > 0:
await ctx.send(f'User {member} will be unmuted in {duration} minutes.')
await asyncio.sleep(duration * 60)
await member.remove_roles(muted_role)
await ctx.send(f'User {member} has been unmuted.')
@bot.command(name='unmute', pass_context=True)
@commands.has_permissions(manage_roles=True)
async def unmute(ctx, member: discord.Member):
muted_role = discord.utils.get(ctx.guild.roles, name='Muted')
await member.remove_roles(muted_role)
await ctx.send(f'User {member} has been unmuted.')
@kick.error
@ban.error
@unban.error
@mute.error
@unmute.error
async def command_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You do not have the required permissions to run this command.")
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send("Please specify the user and reason (if applicable).")
else:
await ctx.send("An error occurred.")
@bot.command()
async def dm(ctx, user: discord.User, *, message: str):
"""Sends a DM to the specified user. Restricted to user groov21."""
if ctx.author.id == 1270322401560891463:
try:
await user.send(message)
await ctx.send(f"Message sent to {user.name}")
except discord.Forbidden:
await ctx.send("I cannot send a message to this user.")
else:
await ctx.send("You are not authorized to use this command.")
@bot.event
async def on_message(message):
# Check if the message is a DM
if isinstance(message.channel, discord.DMChannel) and not message.author.bot:
print(f"Received a DM from {message.author} (ID: {message.author.id}): {message.content}")
# Process other commands if any
await bot.process_commands(message)
@bot.command()
async def meme(ctx):
subreddit = reddit.subreddit('memes')
posts = list(subreddit.hot(limit=100)) # Fetch the top 50 hot posts
post = random.choice(posts) # Choose a random post
if not post.over_18: # Check if the post is not NSFW
embed = discord.Embed(title=post.title, color=discord.Color.blue())
embed.set_image(url=post.url)
embed.set_footer(text=f"👍 {post.score} | 💬 {post.num_comments} comments")
await ctx.send(embed=embed)
else:
await ctx.send("Sorry, this meme is NSFW!")
bot.run('TOKEN')