-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode_discord.py
More file actions
161 lines (133 loc) · 4.9 KB
/
Code_discord.py
File metadata and controls
161 lines (133 loc) · 4.9 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
'''
discord bot
28/6/2020
kartik tripathi
'''
#importing modules
#sdfs
from abc import get_cache_token
import discord
import random
import json
from discord import user
from discord import channel
from discord.ext import commands
from discord import voice_client
from discord.ext.commands.core import guild_only
#making bot (intent are the previlage esclation for the bot so that it can access the member lisst or the the gluid)
# intents = discord.Intents(messages = True, guilds = True, reactions = True, members = True, presences = True)
def get_prefix(client, message):
with open('prefixes.json','r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
# defing bot
client = commands.Bot(command_prefix = get_prefix)
# assigning the prefix according to server.
@client.event
async def on_ready():
await client.change_presence(status=discord.Status.idle, activity=discord.Game('Life is Chilled Out'))
print("Bot's up and running")
@client.event
async def on_guild_join(guild):
with open('prefixes.json','r') as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = '. '
with open('prefixes.json','w') as f:
json.dump(prefixes, f, indent=4)
@client.event
async def on_guild_remove(guild):
with open('prefixes.json','r') as f:
prefixes = json.load(f)
prefixes.pop(str(guild.id))
with open('prefixes.json','w') as f:
json.dump(prefixes,f,indent=4)
@client.command()
async def change_prefix(ctx, prefix):
with open('prefixes.json','r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open('prefixes.json','w') as f:
json.dump(prefixes, f, indent = 4)
await ctx.send(f'prefix changed to {prefix}')
#assigning tasks
@client.command()
async def ping(ctx):
await ctx.send(f'pong!{round(client.latency*1000)}ms')
@client.event
async def on_member_join(member):
print(f'{member} has joined')
@client.event
async def on_member_remove(member):
print(f'{member} has been removed')
@client.command()
async def clear(ctx,amount=5):
await ctx.channel.purge(limit=amount)
@client.command()
async def ban(ctx,member:discord.member,*,reason=None):
await member.ban(reason=reason)
await ctx.send(f'Banned {member.mention}')
@client.command()
async def unban(ctx,*,member):
banned_user = 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'Unbanned{user.mention}')
return
@client.command(aliases = ['8ball,test'])
async def _8ball(ctx, *, question):
responces = ["It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes - definitely.",
"You may rely on it.",
"As I see it, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again.",
"Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful."]
await ctx.send(f'Question:{question}\n Answer: {random.choice(responces)}')
@client.command(pass_context = True)
async def join(ctx):
if(ctx.author.voice):
channel = ctx.message.author.voice.channel
await channel.connect()
else:
await ctx.send('no one in the voice chat!')
@client.command(pass_context = True)
async def leave(ctx):
if (ctx.voice_client): # checks if bot is in vc
await ctx.send("i left the voice channel as you said")
await ctx.guild.voice_client.disconnect()
else:
await ctx.send("i haven't joined")
@client.command()
async def make_channel(ctx):
guild = ctx.guild
member = ctx.author
# admin_role = get(guild.roles, name="Admin")
overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
guild.me: discord.PermissionOverwrite(read_messages=True),
# admin_role: discord.PermissionOverwrite(read_messages=True)
}
channel = await guild.create_text_channel('secret', overwrites=overwrites)
'''
You can use Guild.create_text_channel to create a text channel with certain permissions overwrites.
The below creates a channel that is visible only to the caller, the bot, and members with the "Admin" role
(You'll need to change that to the appropriate role for your server)
'''
# calling bot
client.run('ODU4NjA0NDk3NzY4MDIyMDI2.YNgjwA.hnCHalRaE4vJuStMOzkR58Z5YjI')