Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
chatBotProject/discordBot.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
90 lines (71 sloc)
3.36 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#imports | |
import asyncio #Asyncio used to run the co-routines that are used in the discord library | |
#discord imports used to intergrate the bot with the discord client | |
from discord import Game | |
from discord.ext import commands | |
import discord | |
import youtube_dl #youtube_dl downloads and plays specific youtube urls using the opus libraries ffmpeg | |
import random | |
import requests | |
from lxml import html | |
from discord.utils import get | |
#from googlesearch.googlesearch import GoogleSearch | |
BOT_PREFIX = ("?") | |
# token would be where you put your bot's token to join as the client but the code is going to be public so it's been removed | |
TOKEN = "ommited" | |
client = commands.Bot(command_prefix=BOT_PREFIX) | |
extensions=["Fun","Music","Random","LocationWeather","User","Filter"] | |
async def start(self): | |
"""loads the opus libraries so that it can output audio on voice channels""" | |
discord.opus.load_opus(self.opus_library) | |
# used guide, loads and unloads cogs made in other files https://www.youtube.com/watch?v=gxKM6J5VmIc&t=708s | |
############################################################################################ | |
@client.command(brief="loads command categories back") | |
async def load(extension): | |
try: | |
client.load_extension(extension) | |
except Exception as error: | |
print("{} cannot be loaded [{}] ".format(extension, error)) | |
@client.command(brief="unloads command categories") | |
async def unload(extension): | |
try: | |
client.unload_extension(extension) | |
except Exception as error: | |
print("{} cannot be unloaded [{}] ".format(extension, error)) | |
############################################################################################ | |
#end of code from https://www.youtube.com/watch?v=gxKM6J5VmIc&t=708s | |
#started writing code using googlesearch library but realised it was for python2 | |
"""@client.command(name="g", | |
brief="googles stuff", | |
pass_context=True) | |
async def google(ctx, *search : str): | |
search = "{}".format(" ".join(search)) | |
response = GoogleSearch().search(search) | |
for result in response.results: | |
title = ("Title: " + result.title) | |
content = ("Content: " + result.getText()) | |
await client.say("```{}\n{}```".format(title,content))""" | |
@client.event | |
async def on_ready(): | |
# says that the bot is playing a game called name | |
await client.change_presence(game=Game(name="Gnot a gnoblin|?help")) | |
# prints logged in in the terminal for the code runner to know that they logged on | |
print("Logged in as " + client.user.name) | |
# used guide, loads cogs made in other files when the bot opens https://www.youtube.com/watch?v=gxKM6J5VmIc&t=708s | |
############################################################################################# | |
if __name__ == "__main__": | |
for extension in extensions: | |
try: | |
client.load_extension(extension) | |
except Exception as error: | |
print("{} cannot be loaded [{}] ".format(extension, error)) | |
############################################################################################# | |
#ends using guide https://www.youtube.com/watch?v=gxKM6J5VmIc&t=708s | |
@client.event | |
async def on_message(message): | |
# makes it so the bot doesn't reply to itself | |
if message.author == client.user: | |
return | |
# overwrites the on_message so that it can listen for bot commands | |
await client.process_commands(message) | |
client.run(TOKEN) |