Skip to content
Permalink
2f85e7af45
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
154 lines (123 sloc) 5.39 KB
#########################################
# Bartek's code for the chatbot project #
#########################################
# GROUP E6
# discord.py documentation: https://discordpy.readthedocs.io/en/latest/api.html
# Template used: https://www.devdungeon.com/content/make-discord-bot-python
import discord
from discord.ext import commands
import asyncio
TOKEN = 'NTAzOTk5OTQ1NTUyOTUzMzY0.DrHWKg.6zUjXIIn0qoFEuVQOKEY8jSJifo'
client = discord.Client()
#client = commands.Bot(command_prefix = "!")
###############################
# My own functions/coroutines #
###############################
async def chatbot_log(command, message):
"""Creates a message in the terminal, stating the details of the command used."""
msg_1 = str(message.timestamp) + ": " + message.author.nick + " used " + command
msg_2 = "Server: " + message.server.name
print(msg_1)
print(msg_2)
print("------")
# currently not working
async def terminal_commands():
"""Allows sending commands through the chatbot, managing the bot and testing it."""
chatbot_running = True
while chatbot_running:
command = input("<Chatbot>: ")
# splitting the command will allow me to write commands which use many parameters.
arguments = command.split(" ")
if arguments[0][0:1] != "!":
# gets the chatbot channel class
#chatbot_channel = discord.utils.get(server.channels, name="chatbot")
# send a message to the chatbot channel
#await client.send_message(chatbot_channel, command)
print("<Command>: Message sent.")
else:
if arguments[0] == "!logout":
print("<Command>: Logging out.")
chatbot_running = False
await client.logout()
else:
print("<Command>: Unknown command.")
@client.command(pass_context = True)
async def chatbot_conv(ctx):
#@client.command(pass_context=True)
#async def conversation(ctx):
# msg = "Hello, @" + ctx.message.author.nick + "! How are you today?"
#########################
# discord.py coroutines #
#########################
# discord.py coroutine, runs when the bot joins a server.
@client.event
async def on_server_join(server):
print("Joining " + server.name)
# gives the chatbot its own role (WIP)
#chatbot_role = await client.create_role(server)
#await client.edit_role(server, chatbot_role, name="Chatbot", colour=discord.Colour.gold(), hoist=True)
#chatbot_member = discord.utils.get(server.members, nick="Chatbot")
#await asyncio.sleep(2)
#await client.add_roles(chatbot_member, chatbot_role)
# creates a chatbot channel
await client.create_channel(server, "chatbot")
msg = "Hello @everyone ! Please use this channel to use Chatbot."
await asyncio.sleep(2)
chatbot_channel = discord.utils.get(server.channels, name="chatbot")
await client.send_message(chatbot_channel, msg)
print("Finished joining " + server.name)
# discord.py coroutine, runs when a message is sent on a channel.
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
# we also only want the bot to send messages in the specified channel.
if message.author == client.user or message.channel.name != "chatbot":
return
if message.content.startswith("!hello"):
# {0.author.mention} tags the user that used the command.
msg = 'Hello {0.author.mention}'.format(message)
await client.send_message(message.channel, msg)
await chatbot_log("!hello", message)
elif message.content.startswith("!leave"):
msg = "Goodbye for now!"
await client.send_message(message.channel, msg)
await asyncio.sleep(10)
# deletes channel the bot created when it joined to avoid any potential issues...
# ...with a channel by the same name already existing.
await client.delete_channel(discord.utils.get(message.server.channels, name="chatbot"))
await client.leave_server(message.server)
await chatbot_log("!leave", message)
elif message.content.startswith("!logout"):
msg = "Logging off!"
await client.send_message(message.channel, msg)
await chatbot_log("!logout", message)
await client.logout()
elif message.content.startswith("!pm"):
msg = "Hello, {0.author.mention}! This is a private message.".format(message)
await client.send_message(message.author, msg)
await chatbot_log("!pm", message)
elif message.content.startswith("!conv"):
await chatbot_log("!conv", message)
positive_words = ["good", "alright", "great", "fantastic"]
negative_words = ["bad", "terrible", "awful", "sad"]
msg = "Hello, {0.author.mention}! How are you today?".format(message)
await client.send_message(message.channel, msg)
reply = await client.wait_for_message(author=message.author, channel=message.channel)
reply_words = reply.content.lower().split(" ")
for word in reply_words:
if word in positive_words:
msg = "That's good to hear, {0.author.mention}!".format(message)
await client.send_message(message.channel, msg)
reply_positive = True
break
elif word in negative_words:
msg = "Oh, I hope you feel better soon, {0.author.mention}!".format(message)
await client.send_message(message.channel, msg)
reply_positive = False
break
@client.event
async def on_ready():
print("Logged in as " + client.user.name + ", ID: " + client.user.id)
print("------")
#await terminal_commands()
client.run(TOKEN)