Discord bot using discord.py
Discord is a VOIP and text messaging application that many people use. It has the ability to create 'servers', where text channels and voice channels can be create so people can communicate using it. A bot can be made for Discord, using a multitude of languages including python, so I decided to have a shot at it. Most of it is simple, as python and discord do all the bot stuff for you, you just have to make the commands. So I made some simple commands, such as 8ball question and answer, coinflip, deleting a number of messages, sending private messages to specific users. Then I made 2 bigger programs, which take a group of people and assigns each person another person without repeats, which is useful for some games and/or something like secret santa. Then I made a team organizer, which puts a group of people into a given number of teams. I also allowed it to join VC and leave it, for the purpose of it trying to play music. However, most of the tutorials I've watched on it is from 2018, which is an older version of discord.py, and the documentation for Youtube_Dl has been taken down by DMCA so I don't really know what to do, so it's been abandoned for now. I also created a logging system so I know what the bots being used for.
# Version 1.1
import discord
import random
from datetime import datetime, date
from discord.ext import commands
client = commands.Bot(command_prefix='Kevin ')
today = date.today()
def time():
now = datetime.now()
return now.strftime("[%H:%M:%S] --- ")
@client.event
async def on_ready():
print("Big Bot is online.")
logfile = open(today.strftime("%d%m%Y"), "a")
logfile.write(time() + "Big Bot is online.")
logfile.close()
@client.command(description="Replys with the time taken to respond in ms.")
async def ping(ctx):
latency = round(client.latency*1000)
await ctx.send(f"Pong! {latency}ms")
print("Ping command, with latency:", latency)
logfile = open(today.strftime("%d%m%Y"), "a")
logfile.write("\n" + time() + "Ping command, with latency:" + str(latency))
logfile.close()
@client.command(aliases=['8ball'], description="Gives you an 8ball reply.")
async def _8ball(ctx, *, question):
responses = ["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."]
reply = random.choice(responses)
await ctx.send(reply)
print("8Ball result:", reply)
logfile = open(today.strftime("%d%m%Y"), "a")
logfile.write("\n" + time() + "8Ball result: " + reply)
logfile.close()
@client.command(aliases=['coin', 'headsortails', 'heads', 'tails'], description="Flips a coin.")
async def coinflip(ctx):
results = ["Heads.", "Tails."]
reply = random.choice(results)
await ctx.send(reply)
print("Heads or tails, result:", reply)
logfile = open(today.strftime("%d%m%Y"), "a")
logfile.write("\n" + time() + "Heads or tails, result: " + reply)
logfile.close()
@client.command(aliases=['prune'], description="Deletes the most recent messages. The number does not count the messag"
"e itself.")
async def clear(ctx, amount=0):
await ctx.channel.purge(limit=amount+1)
print("Clearing", amount, "lines.")
logfile = open(today.strftime("%d%m%Y"), "a")
logfile.write("\n" + time() + "Clearing " + str(amount) + " lines.")
logfile.close()
@client.command(description="Warns a user.")
async def warn(ctx, member: discord.Member):
msg = "You have 2 weeks."
await ctx.channel.purge(limit=1)
await member.send(msg)
print("Warning:", str(member))
logfile = open(today.strftime("%d%m%Y"), "a")
logfile.write("\n" + time() + "Warning: " + str(member))
logfile.close()
@client.command(aliases=['Paint'],
description="Gives a user another user in the group, with no repeats. "
"Separate names with only a single comma.")
async def paint(ctx, memberlist=""):
print("Paint command received, string:", memberlist)
logfile = open(today.strftime("%d%m%Y"), "a")
logfile.write("\n" + time() + "Paint command received, string: " + memberlist)
if memberlist == "":
await ctx.send("No names were provided.")
else:
members = []
while memberlist != "":
if "," in memberlist:
for i in range(0, len(memberlist)):
if memberlist[i] == ",":
members.append(memberlist[0:i])
memberlist = memberlist[i + 1:]
break
else:
members.append(memberlist)
memberlist = ""
print("Provided members:", members)
logfile.write("\n" + time() + "Provided members: " + str(members))
while True:
reset = 0
results = []
used = []
for x in range(0, len(members)):
choice = random.choice(members)
while members[x] == choice or choice in used:
choice = random.choice(members)
if len(used) == len(members) - 1 and members[x] == choice:
reset = 1
break
results.append(members[x] + " --> ||" + choice + "||")
used.append(choice)
if reset == 0:
break
print("------Results------")
logfile.write("\n ------Results------")
for x in results:
await ctx.send(x)
print(x)
logfile.write("\n " + x)
logfile.close()
@client.command(aliases=['team', 'split', 'Team', 'Teams', 'Split'],
description="Splits given people into given number of teams. Separate names with only a single comma.")
async def teams(ctx, amount, memberlist):
print("Teams: String received:", memberlist)
logfile = open(today.strftime("%d%m%Y"), "a")
logfile.write("\n" + time() + "Teams: String received: " + memberlist)
members = []
result = []
amount = int(amount)
while memberlist != "":
if "," in memberlist:
for i in range(0, len(memberlist)):
if memberlist[i] == ",":
members.append(memberlist[0:i])
memberlist = memberlist[i + 1:]
break
else:
members.append(memberlist)
memberlist = ""
print("Splitting people into teams, with members:", members)
logfile.write("\n" + time() + "Splitting people into teams, with members: " + str(members))
i = 0
while i < amount:
result.append("")
i += 1
while members:
for x in range(0, amount):
choice = random.choice(members)
result[x] += choice + " "
i = 0
while i < len(members):
if choice == members[i]:
del members[i]
i += 1
if not members:
break
for x in range(0, len(result)):
await ctx.send("Team number " + str(x+1) + ": " + result[x])
print("Team number " + str(x+1) + ": " + result[x])
logfile.write("\n " + "Team number " + str(x+1) + ": " + result[x])
logfile.close()
@client.command(aliases=['whatis', 'define'])
async def commands(ctx, arg=""):
if arg == "":
await ctx.send_help()
print("Commands were printed.")
logfile = open(today.strftime("%d%m%Y"), "a")
logfile.write("\n" + time() + "Commands were printed.")
logfile.close()
else:
await ctx.send_help(arg)
print("Specific command was printed:", arg)
logfile = open(today.strftime("%d%m%Y"), "a")
logfile.write("\n" + time() + "Specific command was printed: " + arg)
logfile.close()
client.remove_command('help')
@client.command(aliases=['Help', 'helpme', 'Helpme'])
async def help(ctx):
await ctx.send("No.")
print("Someone asked for help.")
logfile = open(today.strftime("%d%m%Y"), "a")
logfile.write("\n" + time() + "Someone asked for help.")
logfile.close()
@client.command(aliases=['connect'])
async def join(ctx):
channel = ctx.author.voice.channel
await channel.connect()
print("Connected to voice channel.")
logfile = open(today.strftime("%d%m%Y"), "a")
logfile.write("\n" + time() + "Connected to voice channel.")
logfile.close()
@client.command(aliases=['disconnect', 'quit'])
async def leave(ctx):
await ctx.voice_client.disconnect()
print("Left voice channel.")
logfile = open(today.strftime("%d%m%Y"), "a")
logfile.write("\n" + time() + "Left voice channel.")
logfile.close()
# players = {}
# @client.command()
# async def play(ctx, url):
# try:
# await join(ctx)
# except:
# pass
# guild = ctx.message.guild
# voice_client = guild.voice_client
# voice_client.play()
client.run('NzcyNDIxNjE2NzI2NTczMDU2.X56bsA.sblhHkaEaloJtZeQq3AXptPYojQ')
Logs:[13:48:41] --- Big Bot is online.
[13:48:49] --- Connected to voice channel.
[13:48:50] --- Left voice channel.
[13:48:53] --- Heads or tails, result: Tails.
[13:48:54] --- Heads or tails, result: Tails.
[13:48:57] --- 8Ball result: Don't count on it.
[13:49:14] --- Teams: String received: asjaj,
[13:49:14] --- Splitting people into teams, with members: ['asjaj']
Team number 1: asjaj
Team number 2:
[13:49:39] --- Paint command received, string: 123,234,345,36,457,58
[13:49:39] --- Provided members: ['123', '234', '345', '36', '457', '58']
------Results------
123 --> ||36||
234 --> ||345||
345 --> ||58||
36 --> ||123||
457 --> ||234||
58 --> ||457||
[13:49:45] --- Someone asked for help.
[13:49:48] --- Specific command was printed: warn
[13:49:51] --- Specific command was printed: paint
[13:49:52] --- Heads or tails, result: Heads.
[13:49:56] --- Connected to voice channel.
[13:49:56] --- Left voice channel.[13:56:44] --- Big Bot is online.
[13:57:08] --- Heads or tails, result: Tails.
[13:57:12] --- Heads or tails, result: Heads.[13:48:41] --- Big Bot is online.[13:48:49] --- Connected to voice channel.
[13:48:50] --- Left voice channel.
[13:48:53] --- Heads or tails, result: Tails.
[13:48:54] --- Heads or tails, result: Tails.
[13:48:57] --- 8Ball result: Don't count on it.
[13:49:14] --- Teams: String received: asjaj,
[13:49:14] --- Splitting people into teams, with members: ['asjaj']
Team number 1: asjaj
Team number 2:
[13:49:39] --- Paint command received, string: 123,234,345,36,457,58
[13:49:39] --- Provided members: ['123', '234', '345', '36', '457', '58']
------Results------
123 --> ||36||
234 --> ||345||
345 --> ||58||
36 --> ||123||
457 --> ||234||
58 --> ||457||
[13:49:45] --- Someone asked for help.
[13:49:48] --- Specific command was printed: warn
[13:49:51] --- Specific command was printed: paint
[13:49:52] --- Heads or tails, result: Heads.
[13:49:56] --- Connected to voice channel.
[13:49:56] --- Left voice channel.[13:56:44] --- Big Bot is online.
[13:57:08] --- Heads or tails, result: Tails.
[13:57:12] --- Heads or tails, result: Heads.
Comments
Post a Comment