Advertisement
OrFeAsGr

AI writes a Discord/IRC relay

Dec 12th, 2022
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. Write a simple example code for a Discord bot
  2.  
  3. Here is a simple example code for a Discord bot written in Python:
  4.  
  5. import discord
  6.  
  7. client = discord.Client()
  8.  
  9. @client.event
  10. async def on_ready():
  11. print('Bot is ready')
  12.  
  13. @client.event
  14. async def on_message(message):
  15. if message.author == client.user:
  16. return
  17.  
  18. if message.content == '!hello':
  19. await message.channel.send('Hello!')
  20.  
  21. client.run('YOUR_BOT_TOKEN')
  22.  
  23. This code creates a Discord bot using the discord Python library. When the bot is ready, it will print a message to the console. When the bot receives a message that begins with !hello, it will respond by sending a message that says "Hello!" to the same channel.
  24.  
  25. To use this code, you will need to replace YOUR_BOT_TOKEN with your bot's token, which you can obtain from the Discord developer portal. This code is intended to be a simple example, and it can be expanded and customized to add additional functionality to your Discord bot.
  26. Orfeas Draws
  27. Now write a simple code example for an IRC bot
  28.  
  29. Here is a simple example code for an IRC bot written in Python:
  30.  
  31. import socket
  32.  
  33. server = 'irc.example.com'
  34. channel = '#test'
  35. nickname = 'my_bot'
  36.  
  37. irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  38. irc.connect((server, 6667))
  39. irc.send(f'USER {nickname} {nickname} {nickname} :My Bot\n'.encode('utf-8'))
  40. irc.send(f'NICK {nickname}\n'.encode('utf-8'))
  41. irc.send(f'JOIN {channel}\n'.encode('utf-8'))
  42.  
  43. while True:
  44. data = irc.recv(4096).decode('utf-8')
  45. if data.startswith('PING'):
  46. irc.send('PONG\n'.encode('utf-8'))
  47. elif '!hello' in data:
  48. irc.send(f'PRIVMSG {channel} :Hello!\n'.encode('utf-8'))
  49.  
  50. This code creates an IRC bot that connects to the server at irc.example.com and joins the #test channel. The bot uses the nickname my_bot. When the bot receives a message that contains the string !hello, it will respond by sending a message that says "Hello!" to the same channel.
  51.  
  52. To use this code, you will need to replace irc.example.com with the address of the IRC server you want to connect to, and #test with the name of the channel you want to join. You can also customize the bot's nickname by changing the nickname variable. This code is intended to be a simple example, and it can be expanded and customized to add additional functionality to your IRC bot.
  53. Orfeas Draws
  54. Now write a function in the Discord bot example that is called from the IRC bot when an IRC channel message is sent and takes the message as an argument, and sends it to all the Discord channels.
  55.  
  56. Here is an example of a function that can be added to the Discord bot code from the previous example to send messages from an IRC channel to all Discord channels:
  57.  
  58. import discord
  59.  
  60. client = discord.Client()
  61.  
  62. @client.event
  63. async def on_ready():
  64. print('Bot is ready')
  65.  
  66. @client.event
  67. async def on_message(message):
  68. if message.author == client.user:
  69. return
  70.  
  71. if message.content == '!hello':
  72. await message.channel.send('Hello!')
  73.  
  74. def send_irc_message_to_discord(message):
  75. for guild in client.guilds:
  76. for channel in guild.text_channels:
  77. await channel.send(message)
  78.  
  79. client.run('YOUR_BOT_TOKEN')
  80.  
  81. This code adds a new function, send_irc_message_to_discord(), which takes a message as an argument and sends it to all the Discord channels that the bot is a member of. This function can be called from the IRC bot code when a message is received in the IRC channel, and it will forward the message to all Discord channels.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement